﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace VIFON.DAL
{
    public class DAProduct
    {
        /// <summary>
        /// Get list Product
        /// </summary>
        /// <returns></returns>
        internal List<Product> GetProducts(string keyWord)
        {
            try
            {
                var db = new DBVIFONDataContext();
                {
                    IQueryable<Product> temp = db.Products;
                    if (!string.IsNullOrEmpty(keyWord))
                    {
                        temp = temp.Where(o => o.ProductName.IndexOf(keyWord) != -1);
                    }
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Insert new Product
        /// </summary>
        /// <param name="boProduct"></param>
        /// <returns></returns>
        internal bool Insert(Product boProduct)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    db.Products.InsertOnSubmit(boProduct);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Update Product
        /// </summary>
        /// <param name="boProduct"></param>
        /// <returns></returns>
        internal bool Update(Product boProduct)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Products.FirstOrDefault(o => o.Id == boProduct.Id);
                    if (temp == null)
                        return false;
                    temp.Prices = boProduct.Prices;
                    temp.ProductName = boProduct.ProductName;
                    temp.Unit = boProduct.Unit;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Delete SanPham
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal bool Delete(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Products.FirstOrDefault(o => o.Id == id);
                    if (temp == null)
                        return false;
                    db.Products.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
